import {showNotify, showSuccessToast, showConfirmDialog} from 'vant'
import api from '../../http/api'
import router from '../../router'


const SET_INFO = 'SET_INFO'
const SET_QR_CONTENT = 'SET_QR_CONTENT'
const SET_DOCTOR_LIST = 'SET_DOCTOR_LIST'
const SET_DOCTOR_TASK_INFO = 'SET_DOCTOR_TASK_INFO'
const SET_DOCTOR_REG_COUNT = 'SET_DOCTOR_REG_COUNT'
const SET_DOCTOR_REG_COUNT_MINUS = 'SET_DOCTOR_REG_COUNT_MINUS'
const SET_APPROVE_STATUS = 'SET_APPROVE_STATUS'

export default {
    namespaced: true,
    state: {
        info: {
            id: '',
            accountId: '',
            pid: '',
            name: '',
            phone: '',
            idNum: '',
            initial: '',
            qrContent: '',
            role: '',
            roleDesc: '',
            //实践讲者已注册数量
            shijianReg: '',
            //分配的实践讲者数量
            shijianAllot: '',
            //课题讲者已注册数量
            ketiReg: '',
            //分配的课题讲者数量
            ketiAllot: '',
            //主席的注册数量
            dianpingReg: '',
            //分配的主席数量
            dianpingAllot: '',
            approveStatus: '',
            approveStatusDesc: '',
            because: '',
            signFile: '',
            umuCertFile: []
        },
        doctorList: [],
        meetingList: [],
        doctorTaskInfo: {},
        allotDoctorTag: {}
    },

    getters: {
		getQrContent: state => {
            return state.info.qrContent
        },
        canAllotTag: (state) => (tagCode) => {
            if(tagCode === 1) {
                return state.info.shijianAllot-state.info.shijianReg
            }else if(tagCode === 2) {
                return state.info.ketiAllot-state.info.ketiReg
            }else if(tagCode === 3) {
                return state.info.dianpingAllot-state.info.dianpingReg
            }
        }
	},

    mutations: {
		[SET_INFO]: (state, info) =>{
			state.info = info
		},
        [SET_QR_CONTENT]: (state, content) => {
            state.info.qrContent = content
        },
        [SET_DOCTOR_LIST]: (state, list) => {
            state.doctorList = list
        },
        [SET_DOCTOR_TASK_INFO]: (state, taskInfo) => {
            state.doctorTaskInfo = taskInfo
        },
        [SET_DOCTOR_REG_COUNT]: (state, countType) => {
            state.info[countType] += 1
        },
        [SET_DOCTOR_REG_COUNT_MINUS]: (state, countType) => {
            state.info[countType] -= 1
        },
        [SET_APPROVE_STATUS]: (state, status) => {
            state.info.approveStatus = status
        }
    },

    actions: {
        login({commit, state}) {

        },
        getDoctorList({commit, state}) {
            api.getDoctorList().then(res => {
                if(res.code === 0) {
                    commit(SET_DOCTOR_LIST, res.data)
                }
            })
        },
        getDoctorTaskInfo({commit, state}, id) {
            api.getDoctorTaskInfo(id).then(res => {
                if(res.code === 0) {
                    commit(SET_DOCTOR_TASK_INFO, res.data)
                }
            })
        },
        getQr({commit, state}) {
            api.getQr().then(res => {
                if(res.code === 0) {
                    commit(SET_QR_CONTENT, res.data)
                }
            })
        },
        allotDoctorTag({commit, state, dispatch}, params) {
            api.allotDoctorTag(params).then(res => {
                if(res.code === 0) {
                    showNotify({ type: 'success', message: '操作成功' });
                    if(params.tagCode === 1 && res.data.state === 2) {
                        commit(SET_DOCTOR_REG_COUNT, 'shijianReg')
                    }else if(params.tagCode === 2  && res.data.state === 2) {
                        commit(SET_DOCTOR_REG_COUNT, 'ketiReg')
                    }else if(params.tagCode === 3  && res.data.state === 2) {
                        commit(SET_DOCTOR_REG_COUNT, 'dianpingReg')
                    }
                    dispatch('getDoctorTaskInfo', state.doctorTaskInfo.id)
                    dispatch('getDoctorList')
                }
            })
        },
        deleteDoctorPack({commit, state, dispatch}, params) {
            api.deleteDoctorPack(params).then(res => {
                if(res.code === 0) {
                    showNotify({ type: 'success', message: '操作成功' });
                    if(params.tagCode === 1) {
                        commit(SET_DOCTOR_REG_COUNT_MINUS, 'shijianReg')
                    }else if(params.tagCode === 2) {
                        commit(SET_DOCTOR_REG_COUNT_MINUS, 'ketiReg')
                    }else if(params.tagCode === 3) {
                        commit(SET_DOCTOR_REG_COUNT_MINUS, 'dianpingReg')
                    }
                    dispatch('getDoctorTaskInfo', state.doctorTaskInfo.id)
                    dispatch('getDoctorList')
                }
            })
        }
    },
}
